package com.practice.springboot_01_zwj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.IncomeQuestion;
import com.practice.springboot_01_zwj.entity.PageRequest;
import com.practice.springboot_01_zwj.entity.PageResult;
import com.practice.springboot_01_zwj.entity.ThreeHundred;
import com.practice.springboot_01_zwj.mapper.IncomeQuestionMapper;
import com.practice.springboot_01_zwj.mapper.ThreeHundredMapper;
import com.practice.springboot_01_zwj.service.IncomeQuestionService;
import com.practice.springboot_01_zwj.service.ThreeHundredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.BiFunction;

@Service
@Transactional
public class ThreeHundredServiceImpl implements ThreeHundredService {
    @Autowired
    private ThreeHundredMapper dao;

    @Override
    public List<ThreeHundred> selectThreeHundredAll() {
        return dao.selectThreeHundredAll();
    }

    @Override
    public Map<String, Object> selectThreeByCityCode(String cityCode,String yearFilter) {
        return dao.selectThreeByCityCode(cityCode,yearFilter);
    }

    @Override
    public Map<String, Object> selectThreeByProvinceCode(String provinceCode,String yearFilter) {
        return dao.selectThreeByProvinceCode(provinceCode,yearFilter);
    }

    @Override
    public PageResult<ThreeHundred> selectProvinceCodeAll(String provinceCode, ThreeHundred threeHundred, PageRequest pageRequest) {
        // 防御性处理条件对象
        ThreeHundred finalCondition = Optional.ofNullable(threeHundred)
                .orElseGet(ThreeHundred::new);

        // 获取全部数据
        List<ThreeHundred> allData = dao.selectProvinceCodeAll(provinceCode, finalCondition);

        // 执行内存分页
        return paginateData(allData, pageRequest);
    }

    @Override
    public PageResult<ThreeHundred> selectCityCodeAll(
            String cityCode,
            ThreeHundred threeHundred,
            PageRequest pageRequest) {

        // 防御性处理条件对象
        ThreeHundred finalCondition = Optional.ofNullable(threeHundred)
                .orElseGet(ThreeHundred::new);

        // 获取全部数据
        List<ThreeHundred> allData = dao.selectCityCodeAll(cityCode, finalCondition);

        // 执行内存分页
        return paginateData(allData, pageRequest);
    }

    // 通用的内存分页方法（泛型实现）
    private <T> PageResult<T> paginateData(List<T> allData, PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        int totalItems = allData.size();

        // 计算总页数
        int totalPages = (totalItems + pageSize - 1) / pageSize;

        // 确保当前页在有效范围内
        if (pageNum > totalPages) {
            pageNum = totalPages;
        }
        if (pageNum < 1) {
            pageNum = 1;
        }

        // 计算分页起始位置
        int startIndex = (pageNum - 1) * pageSize;
        if (startIndex < 0) startIndex = 0;

        // 处理空数据集情况
        if (startIndex >= totalItems) {
            return new PageResult<>(
                    pageNum,
                    pageSize,
                    (long) totalItems,
                    Collections.emptyList()
            );
        }

        // 计算结束位置
        int endIndex = Math.min(startIndex + pageSize, totalItems);

        // 获取当前页数据
        List<T> pageData = allData.subList(startIndex, endIndex);

        return new PageResult<>(
                pageNum,
                pageSize,
                (long) totalItems,
                pageData
        );
    }

}
